import * as fs from 'fs';
import * as serverConfig from '../config/config';
import * as log from '../log/log';
import * as schedule from 'node-schedule';

export let fileName = 'schedule-out.txt';






/**
 *  参数说明详见：https://www.npmjs.com/package/node-schedule
 * 
 * @param {(schedule.RecurrenceRule | schedule.RecurrenceSpec | Date | string)} rule
 * @param {function} callback
 */
export function scheduleJob(rule: schedule.RecurrenceRule | schedule.RecurrenceSpec | Date | string, callback:()=>void){
    var rule1 = new schedule.RecurrenceRule(); 
    var j = schedule.scheduleJob(rule, function(){
       callback();
    });
}

export function appedFile(_filename){
    let filePath = serverConfig.getAssetPath("config/"+fileName);
    fs.appendFile(filePath,_filename+"\r\n",err=>{
        if(err){
            log.error("schedule appendFile",err,_filename);
        }
    });
}

export function readFile():string{
    return fs.readFileSync(serverConfig.getAssetPath("config/"+fileName),'utf-8');
}
